//
//  ThemeSelectViewController.m
//  WXWeibo
//
//  Created by apple on 16/5/10.
//  Copyright © 2016年 zic_magic. All rights reserved.
//

#import "ThemeSelectViewController.h"
#import "ThemeSelectCell.h"
#import "ThemeManager.h"

@interface ThemeSelectViewController ()
@property (nonatomic, copy) NSMutableArray *mArr;
@end

@implementation ThemeSelectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.rowHeight = 40;
    [self.tableView registerNib:[UINib nibWithNibName:@"ThemeSelectCell" bundle:nil] forCellReuseIdentifier:@"ThemeSelectCell"];
    
    [self loadData];
    [self themeChange];
}

- (void)loadData {
    
    NSDictionary *dic = [NSMutableDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Theme.plist" ofType:nil]];
//    NSLog(@"%@", dic);
    _mArr = [NSMutableArray array];
    for (NSString *str in [dic allKeys]) {
        [_mArr addObject:str];
        
    }

//    NSLog(@"%@", _mArr);
    
}



- (instancetype)initWithCoder:(NSCoder *)aDecoder{
    
    
    self = [super initWithCoder:aDecoder];
    
    if (self) {
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(themeChange) name:kThemeChangeNotification object:nil];
    }
    
    return self;
}

- (void)themeChange{
    

    UIColor *bgColor = [[ThemeManager shareManager] themeColorWithColorName:@"More_Item_color"];
    self.tableView.backgroundColor = bgColor;
    UIColor *lineColor = [[ThemeManager shareManager] themeColorWithColorName:@"More_Item_Line_color"];
    self.tableView.separatorColor= lineColor;
    
}




- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return _mArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    ThemeManager *manager = [ThemeManager shareManager];
    
    ThemeSelectCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ThemeSelectCell" forIndexPath:indexPath];

    cell.backgroundColor = [UIColor clearColor];

    cell.themeName.text = _mArr[indexPath.row];
    cell.themeName.textColor = [manager themeColorWithColorName:@"More_Item_Text_color"];
    
    NSString *themePath = manager.themeDic[_mArr[indexPath.row]];
    NSString *imgPath = [[[NSBundle mainBundle].resourcePath stringByAppendingPathComponent:themePath] stringByAppendingPathComponent:@"more_icon_theme.png"];
    cell.imgView.image = [UIImage imageWithContentsOfFile:imgPath];
    
    
    if([manager.themeName isEqualToString:_mArr[indexPath.row]]){
        
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        
    }else {
        
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    [ThemeManager shareManager].themeName = _mArr[indexPath.row];
    
    [self themeChange];
    [tableView reloadData];
    
    
}


- (void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:kThemeChangeNotification object:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
